package com.gx.smart.building.ui.knowledge.viewmodel

import android.app.Application
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.casic.gx.grpc.common.ComQuery
import com.gx.smart.base.BaseViewModel
import com.gx.smart.lib.http.api.RetrofitManager
import com.gx.smart.lib.http.api.config.ApiConfig
import com.gx.smart.lib.http.api.response.Knowledge
import com.gx.smart.lib.http.api.response.KnowledgeBaseType
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

/**
 *@author: xiaosy
 *时间: 2022/4/7
 *@Description: java类作用描述
 *@Version: 1.0
 **/
class KnowledgeViewModel(app: Application) : BaseViewModel(app) {
    private val list: MutableLiveData<List<KnowledgeBaseType>> = MutableLiveData()
    private val articleList: MutableLiveData<List<Knowledge>> = MutableLiveData()
    private val articleDetail: MutableLiveData<Knowledge> = MutableLiveData()

    //查询文章分类列表
    fun queryKnowledgeTypeTreeList(): LiveData<List<KnowledgeBaseType>> {

        RetrofitManager.getInstance().knowledgeTypeApiService.queryAppKnowledgeTypeTreeList().enqueue(object:Callback<com.gx.smart.lib.http.api.response.QueryKnowledgeTypeTreeListResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeTypeTreeListResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryKnowledgeTypeTreeListResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    list.value = result.knowledgeBaseTypeDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeTypeTreeListResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })

        return list
    }



    //查询文章列表
    fun queryKnowledgeList(currentPage: Int, baseTypeId: String): LiveData<List<Knowledge>> {
        val comQuery = ComQuery
            .newBuilder()
            .setPage(currentPage)
            .setPageSize(10)
            .build()

        RetrofitManager.getInstance().knowledgeApiService.queryAppKnowledgeList().enqueue(object:Callback<com.gx.smart.lib.http.api.response.QueryKnowledgeListResp> {
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeListResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryKnowledgeListResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    articleList.value = result.knowledgeDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeListResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return articleList
    }

    //查看文章详情
    fun queryKnowledgeDetail(id:String): LiveData<Knowledge> {
        RetrofitManager.getInstance().knowledgeApiService.queryAppKnowledgeDetail().enqueue(object: Callback<com.gx.smart.lib.http.api.response.QueryKnowledgeDetailResp>{
            override fun onResponse(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeDetailResp>,
                response: Response<com.gx.smart.lib.http.api.response.QueryKnowledgeDetailResp>
            ) {
                val result = response.body()
                if (result == null) {
                    ToastUtils.showLong("服务器超时")
                    return
                }
                val comResp = result.comResp
                if (comResp.code == 100) {
                    articleDetail.value = result.knowledgeDto
                } else {
                    ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
                }
            }

            override fun onFailure(
                call: Call<com.gx.smart.lib.http.api.response.QueryKnowledgeDetailResp>,
                t: Throwable
            ) {
                ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE)
            }

        })
        return articleDetail
    }
}